package com.xiang.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiang.pojo.Book;
import com.xiang.pojo.ResultVo;
import com.xiang.service.BookService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/book")
public class BookServlet extends BaseServlet {

    BookService bs = new BookService();
    private ResultVo vo = null;

    //查询全部图书信息
    public void findAllBooks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //业务处理
        List<Book> bList = bs.findAllBooks();
        //判断
        if (bList != null && bList.size() > 0) {
            //统一处理前后台交互的规范
            vo = new ResultVo(1, "查询成功", bList);
        } else {
            vo = new ResultVo(0, "查询失败", bList);
        }
        //将集合转json
        String s = new ObjectMapper().writeValueAsString(vo);
        //响应到页面展示
        response.getWriter().print(s);
    }

    //新增图书信息
    public void addBook(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取从前端传来的图书信息
        Map<String, String[]> map = request.getParameterMap();
        //封装到对象
        Book book = new Book();
        //需要借助于BeanUtils工具类,将map集合封装到book对象中
        BeanUtils.populate(book, map);
        //业务处理
        boolean flag = bs.addBook(book);
        if (flag) {
            vo = new ResultVo(1, "添加成功", flag);
        } else {
            vo = new ResultVo(0, "添加失败", flag);
        }
        //将集合转json
        String s = new ObjectMapper().writeValueAsString(vo);
        //响应到页面展示
        response.getWriter().print(s);
    }

    //根据bid查询图书对象信息
    public void findBookByBid(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String bid = request.getParameter("bid");
        Book book = bs.findBookByBid(bid);
        if (book != null) {
            vo = new ResultVo(1, "查询成功", book);
        } else {
            vo = new ResultVo(0, "查询失败", book);
        }
        //将集合转json
        String s = new ObjectMapper().writeValueAsString(vo);
        //响应到页面展示
        response.getWriter().print(s);
    }

    //修改图书信息
    public void updateBook(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取从前端传来的图书信息
        Map<String, String[]> map = request.getParameterMap();
        //封装到对象
        Book book = new Book();
        //需要借助于BeanUtils工具类,将map集合封装到book对象中
        BeanUtils.populate(book, map);
        //业务处理
        boolean flag = bs.updateBook(book);
        if (flag) {
            vo = new ResultVo(1, "修改成功", flag);
        } else {
            vo = new ResultVo(0, "修改失败", flag);
        }
        //将集合转json
        String s = new ObjectMapper().writeValueAsString(vo);
        //响应到页面展示
        response.getWriter().print(s);
    }

    //根据bid删除图书信息
    public void delBookByBid(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String bid = request.getParameter("bid");
        boolean flag = bs.delBookByBid(bid);
        if (flag) {
            vo = new ResultVo(1, "删除成功", flag);
        } else {
            vo = new ResultVo(0, "删除失败", flag);
        }
        //将集合转json
        String s = new ObjectMapper().writeValueAsString(vo);
        //响应到页面展示
        response.getWriter().print(s);
    }
}